const dbutil = require('../../../libs/MyDB');
// const { set_token } = require('../../../middleware/token');
let regphone = /0?(13|14|15|17|18|19)[0-9]{9}/;
module.exports = {

    //用户登录验证
    checkLogin: async function (data) {
        let result = {};
        if (regphone.test(data.username)) {
            await dbutil.singleSelect('study_user', 'password', `phone='${data.username}'`)
                .then(
                    res => {
                        // console.log(res);
                        result = res
                    }
                )
        } else {
            await dbutil.singleSelect('study_user', 'password', `studentId='${data.username}'`)
                .then(
                    res => {
                        // console.log(res);
                        result = res
                    }
                )
        }
        if (!result.password) {
            return {
                code: 400,
                msg: "用户名不存在"
            }
        } else {
            if (result.password != (data.password)) {
                return {
                    code: 402,
                    msg: "密码错误"
                }
            } else {
                let user = {};
                await dbutil.multiSelect('study_userinfo', ['user_detail_id', 'uname', 'sex', 'class_name', 'phone', 'uimg'], `phone='${data.username}' or studentId='${data.username}'`)
                    .then(
                        result => {
                            user = result[0]
                        }
                    )
                return {
                    code: 200,
                    msg: "登录成功",
                    user,
                }
            }
        }
    },

    //用户注册验证
    checkLogup: async function (data) {
        let result = {};
        await dbutil.singleSelect('study_user', 'phone', `phone='${data.username}'`)
            .then(
                res => {
                    // console.log(res);
                    result = res
                }
            )
        if (result.phone) {
            return {
                code: 400,
                msg: "该电话号码存在"
            }
        }
        let uid = '';
        await dbutil.add('study_user', {
            phone: (data.username),
            password: (data.password),
        }).then(
            res => {
                uid = res.insertId;
            }
        )
        await dbutil.add('study_userdetail', {
            user_detail_id: uid,
            uname: (data.username),
            sex: data.sex,
        })
        let studentId = '';
        let year = new Date().getFullYear();//获取年份
        let month = new Date().getMonth() + 1;//获取月份
        let date = new Date().getDate();//获取日期
        if (month < 10) {
            month = "0" + month;//判断月份是否为个位数，如果是添加在前面添加一个0
        }
        if (date < 10) {
            date = "0" + date;//判断日期是否为个位数，如果是添加在前面添加一个0
        }
        if (uid < 10) {
            studentId = "000" + uid;//判断UID是否为个位数，如果是添加在前面添加一个000
        } else if (uid < 100) {
            studentId = "00" + uid;//判断UID是否为个位数，如果是添加在前面添加一个00
        } else if (uid < 1000) {
            studentId = "0" + uid;//判断UID是否为个位数，如果是添加在前面添加一个0
        }
        studentId = year + month + date + studentId;//拼接成学号
        //根据uid修改数据库的学号
        await dbutil.update('study_user', `user_id = ${uid}`, {
            studentId: studentId,
        })

        let user = {};
        await dbutil.multiSelect('study_userinfo', ['user_detail_id', 'uname', 'sex', 'class_name', 'phone', 'uimg'], `phone='${data.username}' or studentId='${data.username}'`)
            .then(
                result => {
                    user = result[0];
                }
            )
        return {
            code: 200,
            msg: "注册成功",
            user,
        }
    },

    //用户头像
    saveImg: async function (data) {
        await dbutil.update('study_user', `user_id = ${data.uid}`, {
            uimg: data.filesName,
        })
        return {
            code: 200,
            msg: '上传文件成功',
            url: data.filesName,
        }
    },
    
    //用户修改个人信息
    updateMsg: async function (data) {
        await dbutil.update('study_user', `user_id = ${data.uid}`, {
            phone: data.inputPhone
        })
        await dbutil.update('study_userdetail', `user_detail_id = ${data.uid}`, {
            uname: data.inputName,
            sex: data.inputRadio,
        })
        let user = {};
        await dbutil.multiSelect('study_userinfo', ['user_detail_id', 'uname', 'sex', 'class_name', 'phone', 'uimg'], `phone='${data.inputPhone}'`)
            .then(
                result => {
                    user = result[0];
                }
            )
        return {
            code: 200,
            msg: "修改成功",
            user,
        }
    }
}